<?php
/**
 * BootAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using BootAdmin.
 *
 * @Author G.r<root@BootAdmin.com>
 */

declare (strict_types=1);

namespace app\admin\logic;

use app\admin\model\Dept;
use app\common\logic\BaseLogic;
use think\db\exception\DbException;

/**
 * 部门逻辑
 */
class DeptLogic extends BaseLogic
{
    protected Dept $model;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->model = new Dept();
    }

    /**
     * 获取列表
     */
    public function list($params, bool $onlyTrashed = false)
    {
        try {
            $list = $this->model->setParams($params)->queryBuilder($onlyTrashed)->select();
            return $this->model->toTree($list->toArray());
        } catch (DbException $e) {
            $this->throw($e->getMessage());
        }
    }

    /**
     * 快捷列表
     */
    public function getList($params, bool $onlyTrashed = false)
    {
        $list = $this->model->setParams($params)->getList($onlyTrashed);
        return $this->model->toTree($list);
    }

    /**
     * 详情
     */
    public function read($id)
    {
        try {
            return $this->model->find($id);
        } catch (DbException $e) {
            $this->throw($e->getMessage());
        }
    }

    /**
     * 新增数据
     * @param $params
     * @return bool
     */
    public function add($params)
    {
        if (isset($params['id']) && $params['id'] == $params['parent_id']) {
            $this->throw('上级部门不能为本部门');
        }
        $pid = $params['parent_id'] ?? 0;

        if ($pid === 0) {
            $params['level'] = $params['parent_id'] = '0';
        } else {
            $parent = $this->model->where('id', $params['parent_id'])->find();
            $params['level'] = $parent->level . ',' . $params['parent_id'];
        }
        $result = $this->model->setParams($params)->add();
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 修改数据
     * @param $params
     * @return bool
     */
    public function edit($params)
    {
        if (isset($params['id']) && $params['id'] == $params['parent_id']) {
            $this->throw('上级部门不能为本部门');
        }
        $pid = $params['parent_id'] ?? 0;

        if ($pid === 0) {
            $params['level'] = $params['parent_id'] = '0';
        } else {
            $parent = $this->model->where('id', $params['parent_id'])->find();
            $params['level'] = $parent->level . ',' . $params['parent_id'];
        }
        $result = $this->model->setParams($params)->edit($params['id']);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 删除数据
     * @param $id
     * @return bool
     */
    public function del($id)
    {
        $result = $this->model->del($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 真实删除数据
     * @param $id
     * @return bool
     */
    public function realDel($id)
    {
        $result = $this->model->realDel($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 恢复数据
     * @param $id
     * @return bool
     */
    public function recovery($id)
    {
        $result = $this->model->recovery($id);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }

    /**
     * 字段修改
     * @param $params
     * @return bool
     */
    public function modify($params)
    {
        $result = $this->model->setParams($params)->modify($params['id']);
        if (!$result) {
            $this->throw($this->model->getError());
        }
        return $result;
    }
}